package edu.zzuli.assistant.api.action;

import java.util.Map;
import org.apache.struts2.interceptor.SessionAware;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import edu.zzuli.assistant.bean.Follow;
import edu.zzuli.assistant.service.FollowService;
import edu.zzuli.assistant.service.impl.FollowServiceImpl;
import edu.zzuli.assistant.until.StrUtil;

public class FollowAction extends ActionSupport implements SessionAware,ModelDriven{
	
	
	private FollowService followService = new FollowServiceImpl();
	private Map<String, Object> session;
	private Follow follow;
	public Follow getFollow() {
		return follow;
	}

	public void setFollow(Follow follow) {
		this.follow = follow;
	}

	public Map<String, Object> getSession() {
		return session;
	}
	public void setSession(Map<String, Object> session) {
		this.session = session;
	}
	
	@Override
	public Object getModel() {
		// TODO Auto-generated method stub
		if (follow!=null) {
			this.follow=new Follow();
			
		}
		return follow;
	}

	//取消关注follow_cancel.action
	public void cancel(){
	
		if (session.get("userId")!=null) {
			 String userId = session.get("userId").toString();
			 if ( follow.getStartId()!=null) {
			     boolean result=followService.deleteById(userId, follow.getStartId());
				 if (result) {
					 StrUtil.sendMsg(SUCCESS);
				 }else {
					 StrUtil.sendMsg("fail");
				 }
			}
			 else{
				 StrUtil.sendMsg(ERROR);
			 }
		}	
		else{
			 StrUtil.sendMsg(LOGIN);
		}
	}
	
	//添加关注follow_add.action
	public void add(){
		if (session.get("userId")!=null) {
			 String userId = session.get("userId").toString();
			 if (follow.getStartId()!=null) {
				 follow.setFansId(userId);
			     boolean result=followService.add(follow);
				 if (result) {
					 StrUtil.sendMsg(SUCCESS);
				 }else {
					 StrUtil.sendMsg("fail");
				 }
			}
			 else{
				 StrUtil.sendMsg(ERROR);
			 }
			
		}
		else{
			 StrUtil.sendMsg(LOGIN);
		}
	}


}
